package com.front.websocket;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: controller
 * Package: com.front.websocket
 * Description:
 *
 * @Author BuTang
 * @Create 2024/1/5 16:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/websocket")
public class controller {
    @Resource
    private WebSocket webSocket;
    /**
     * 页面文件入口
     */
    @GetMapping("/index")
    public String index(){
        String index = "index";
        return index;
    }

    /**
     * 发送场景模拟
     * @param msg
     * @return
     */
    @GetMapping("/test")
    @ResponseBody
    public String sendMessage(String msg){
        //如果访问的地址中msg参数不为空值，发送msg的值给前端
        if(!StringUtils.isEmpty(msg)){
            webSocket.sendMessage(msg);
            return "服务端发送消息msg："+msg;
        }
        return "服务端未发送消息msg："+msg;
    }

}
